package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.GetBindAccountData;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

public class BindAcountActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout mBindAlipay;
    private LinearLayout mBindWeixin;
    private boolean isBindZhiFuBao = false;
    private boolean isBindWeixin = false;
    private TextView mIsBindZhiFuBao;
    private TextView mIsBindWeixin;
    private String mPhone;
    private String token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_account);
        //获取手机号码和token
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        token = sp.getString("token", "");
        mIsBindZhiFuBao = (TextView) findViewById(R.id.isBindZhiFuBao);
        mIsBindWeixin = (TextView) findViewById(R.id.isBindWeixin);
        mBindAlipay = (LinearLayout) findViewById(R.id.bindAlipay);
        mBindWeixin = (LinearLayout) findViewById(R.id.bindWeixin);
        mBindAlipay.setOnClickListener(this);
        mBindWeixin.setOnClickListener(this);
//        BaseActivity.activityStack.push(this);
        getIsBinData();
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.bindAlipay:
                if (isBindZhiFuBao == false) {
                    startActivityForResult(new Intent(BindAcountActivity.this, BindAlipay.class), Constant.BINDALIPAYREQUESRCODE);
                } else if (isBindZhiFuBao) {
                    mIsBindZhiFuBao.setText("已绑定");
                    mIsBindZhiFuBao.setTextColor(getResources().getColor(R.color.loginBtn));
                    startActivityForResult(new Intent(BindAcountActivity.this, BindAlipay.class), Constant.BINDALIPAYREQUESRCODE);
                }
                break;
            case R.id.bindWeixin:
                if (isBindWeixin == false) {
                    startActivityForResult(new Intent(BindAcountActivity.this, BindWeixin.class), Constant.BINDWEIXINREQUESTCODE);
                } else if (isBindWeixin) {
                    mIsBindWeixin.setText("已绑定");
                    mIsBindWeixin.setTextColor(getResources().getColor(R.color.loginBtn));
                    startActivityForResult(new Intent(BindAcountActivity.this, BindWeixin.class), Constant.BINDWEIXINREQUESTCODE);
                }
        }
    }

    private void getIsBinData() {
        OkHttpUtils.post().addParams("phone", mPhone).addParams("token", token)
                .url(Constant.BINDACCOUNTGETDATAREQUESTCODE)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast("连接不上服务器!");
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    GetBindAccountData getBindAccountData = new Gson().fromJson(response, GetBindAccountData.class);
                    String code = getBindAccountData.getCode().trim();
                    if (code.equals(200 + "")) {
                        GetBindAccountData.DataBean data = getBindAccountData.getData();
                        if (data instanceof GetBindAccountData.DataBean) {
                            String playwx = data.getPlaywx().toString().trim();
                            String playzfb = data.getPlayzfb().toString().trim();
                            if (playzfb != null && !playzfb.equals(0 + "")) {
                                isBindZhiFuBao = true;
                                mIsBindZhiFuBao.setText("已绑定");
                                mIsBindZhiFuBao.setTextColor(getResources().getColor(R.color.loginBtn));
                            } else if (playzfb.equals(0 + "")) {
                                isBindZhiFuBao = false;
                                mIsBindZhiFuBao.setText("未绑定");
                            }
                            if (playwx != null && !playwx.equals(0 + "")) {
                                isBindWeixin = true;
                                mIsBindWeixin.setText("已绑定");
                                mIsBindWeixin.setTextColor(getResources().getColor(R.color.loginBtn));
                            } else if (playwx.equals(0 + "")) {
                                isBindWeixin = false;
                                mIsBindWeixin.setText("未绑定");
                            }
                        } else if (data instanceof List<?>) {
                            isBindZhiFuBao = false;
                            isBindWeixin = false;
                        }
                        Log.e("200", "返回成功!" + data);
                    } else if (code.equals(201 + "")) {
                        showToast("非法请求!");
                    } else if (code.equals(202 + "")) {
                        showToast("非法请求!");
                    }
                } else {
                    showToast("数据为空!");
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constant.BINDALIPAYREQUESRCODE && resultCode == Constant.YIJINGBINDZHIFUBAO) {
            isBindZhiFuBao = data.getBooleanExtra("isBindZhiFuBao", false);
            String playzfb = data.getStringExtra("playzfb");
            //判断是否绑定
            if (isBindZhiFuBao) {
                mIsBindZhiFuBao.setText("已绑定");
                mIsBindZhiFuBao.setTextColor(getResources().getColor(R.color.loginBtn));
            } else if (isBindZhiFuBao == false) {
                mIsBindZhiFuBao.setText("未绑定");
            }
        } else if (requestCode == Constant.BINDWEIXINREQUESTCODE && resultCode == Constant.YIJINGBINDWEIXIN) {
            isBindWeixin = data.getBooleanExtra("isBindWeixin", false);
            //判断是否绑定
            if (isBindWeixin) {
                mIsBindZhiFuBao.setTextColor(getResources().getColor(R.color.loginBtn));
                mIsBindZhiFuBao.setText("已绑定");
            } else if (isBindWeixin == false) {
                mIsBindWeixin.setText("未绑定");
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}
